const { ccclass, property } = cc._decorator;

@ccclass
export default class MoveProgressBar extends cc.Component {
    @property(cc.ProgressBar)
    ProgressBar: cc.ProgressBar = null;

    @property(cc.Integer)
    mMoveTime: number = 0.2;

    private mEndRsultCb: Function = null;
    private mTargetProgress: number = 0;
    private mMoveKey: number = 0;

    onLoad() {
        this.resertProgress();
    }

    bindEndCb(endCb: Function) {
        this.mEndRsultCb = endCb;
    }

    getProgress(): cc.ProgressBar {
        return this.ProgressBar;
    }

    setProgress(progress: number) {
        this.mTargetProgress = progress;
        this.mMoveKey = (this.mTargetProgress - this.ProgressBar.progress);

        this.mMoveKey = this.mMoveKey / (this.mMoveTime / 0.01);
        this.ProgressBar.progress = this.mTargetProgress;
        this.schedule(this.updateOpen, 0.01);
    }

    updateOpen() {
        if (Math.abs(this.ProgressBar.progress - this.mTargetProgress) <= 0.01) {
            this.ProgressBar.progress = this.mTargetProgress;
            this.unschedule(this.updateOpen);

            if (this.mEndRsultCb) {
                this.mEndRsultCb();
            }
            return;
        }
        this.ProgressBar.progress += this.mMoveKey;
    }


    initProgress(progress: number) {
        this.ProgressBar.progress = progress;
    }

    resertProgress() {
        this.ProgressBar.progress = 0;
    }

    getMoveProgressTime(): number {
        return this.mMoveTime;
    }
}

